﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Reflection</title>
  <keywords>
    <keyword translate="true">Reflection</keyword>
    <keyword translate="true">TypeHelper.GetType</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Reflection</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Internally, Catel uses lots of reflection to implement some of its behavior. And why not make all these excellent reflection classes public?</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting loaded assemblies</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">In WPF, it is very simple to get all the loaded assemblies. In Silverlight, it already gets a bit harder. Silverlight in combination with modules that are separately loaded are horrible to use via reflection. In Catel, it is possible to register xap files to the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_AssemblyHelper" styleclass="Normal" translate="true">AssemblyHelper</link><text styleclass="Normal" translate="true"> class:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">AssemblyHelper.RegisterAssembliesFromXap(xapStream);</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Catel unpacks the xap (which is basically just a zip file) and adds all the assemblies to an internal list of assemblies. This way, the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_AssemblyHelper_GetLoadedAssemblies" styleclass="Normal" translate="true">AssemblyHelper.GetLoadedAssemblies</link><text styleclass="Normal" translate="true"> method actually returns </text><text styleclass="Normal" style="font-weight:bold;" translate="true">all</text><text styleclass="Normal" style="font-weight:normal;" translate="true"> the loaded assemblies, also the ones in dynamically loaded xaps that are not available by default.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting types in Silverlight</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Sometimes you know what type to get and what assembly it is living in. However, you don&apos;t want to be version-dependent by specifying the fully qualified assembly name. Using the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="M_Catel_TypeHelper_GetType_1_72eac341" styleclass="Normal" translate="true">TypeHelper.GetType</link><text styleclass="Normal" translate="true"> method, it is possible to get a type by only the assembly name (say </text><text styleclass="Normal" style="font-style:italic;" translate="true">Catel.Silverlight</text><text styleclass="Normal" translate="true">) and the type name (say </text><text styleclass="Normal" style="font-style:italic;" translate="true">Catel.Data.ObservableObject</text><text styleclass="Normal" translate="true">).</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var type = PropertyHelper.GetType(&quot;Catel.Data.ObservableObject&quot;,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;Catel.Silverlight&quot;);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Setting or getting properties of objects</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">In lots of cases, you need to possibility to set or get properties of an object via reflection. This behavior is implemented in the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Reflection_PropertyHelper" styleclass="Normal" translate="true">PropertyHelper</link><text styleclass="Normal" translate="true"> class. Below are a few examples.</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Check if a property is available on an object</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PropertyHelper.IsPropertyAvailable(person,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;FirstName&quot;);</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Getting a property value</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PropertyHelper.GetValue(person,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;FirstName&quot;);</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">or</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">string</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">firstName;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PropertyHelper.TryGetValue(person,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;FirstName&quot;,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">out</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">firstName);</text></para>
    <para styleclass="Heading2"><text styleclass="Heading2" translate="true">Setting a property value</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PropertyHelper.SetValue(person,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;FirstName&quot;, &quot;Geert&quot;);</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">or</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">PropertyHelper.TrySetValue(person,</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&quot;FirstName&quot;, &quot;Geert&quot;);</text></para>
  </body>
</topic>
